//
//  RGInviteSecondVC.m
//  RedGrass
//
//  Created by wuyine on 2019/2/13.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGInviteSecondVC.h"
#import "RGInviteRecordsVC.h"
#import "RGShareManager.h"
#import "HttpRequest.h"
#import "XLMShareSdkTool.h"

@interface RGInviteSecondVC ()
@property (nonatomic,strong) UILabel *inviteCodeLabel;
@property (nonatomic,strong) UIButton *inviteRecordsBtn;
@property (nonatomic,strong) UIButton *fuzhiBtn;
@property (nonatomic,strong) UILabel *tip;
@end

@implementation RGInviteSecondVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.NavStyleType = RGCustomNavStyleType_default;
    self.customNavBar.hidden = YES;

    [self initUI];
    [self loadForvateCount];
}

//获取邀请注册的奖励金额
-(void)loadForvateCount{
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/inviteReward"];
    AA_WEAK_MAKE(self)weakSelf = self;
    [self showLoading:@""];
    [quest post:@{} success:^(id data) {
        [weakSelf dismissLoading];
        NSDictionary *dic = [data objectForKey:@"inviteReward"];
        if(dic){
            NSString *dicValue = [dic objectForKey:@"dicValue"];
            [weakSelf updateForvateCountWithStr:dicValue];
        }
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf dismissLoading];
    }];
}

-(void) updateForvateCountWithStr:(NSString *)str{
    if([str isKindOfClass:[NSString class]] && str.length != 0){
        _tip.text = [NSString stringWithFormat:@"好友注册后你将获得%@元现金红包",str];
    }
}

- (void)initUI {
    
    UIImageView *bg = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"二维码_bj")];
    bg.userInteractionEnabled = YES;
    [self.customContentView addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
        //        make.top.equalTo(canzhao.mas_bottom).offset(0);
        //        make.top.mas_equalTo(-SafeAreaTopHeight);
    }];
    
    UIScrollView *scroll = [[UIScrollView alloc] init];
    scroll.backgroundColor = [UIColor clearColor];
    [self.customContentView addSubview:scroll];
    [scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    UIView *canzhao = [[UIView alloc] init];
    [scroll addSubview:canzhao];
    [canzhao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(scroll.mas_width);
        make.height.mas_equalTo(0.5);
        make.leading.trailing.top.mas_equalTo(0);
    }];
    
    UIView *shu = [[UIView alloc] init];
    [scroll addSubview:shu];
    [shu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(0.5);
        make.height.equalTo(scroll.mas_height).priority(250);
    }];
    
    UIImageView *head = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"分享有礼_图案")];
    [scroll addSubview:head];
    [head mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(0);
        make.top.equalTo(canzhao.mas_bottom).offset(KIsiPhoneX?44:22);
        make.height.mas_equalTo(KScreenWidth*490/375);
    }];
    
    [scroll addSubview:self.inviteRecordsBtn];
    [_inviteRecordsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(head.mas_top).offset(10);
        make.trailing.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(100, 25));
    }];
    
    [scroll addSubview:self.inviteCodeLabel];
    [_inviteCodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(-30);
        make.top.equalTo(head.mas_bottom).offset(20);
    }];
    
    [scroll addSubview:self.fuzhiBtn];
    [_fuzhiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.inviteCodeLabel.mas_centerY);
        make.leading.equalTo(self.inviteCodeLabel.mas_trailing).offset(8);
        make.size.mas_equalTo(CGSizeMake(60, 20));
    }];
    
    UILabel *tip = [[UILabel alloc] init];
    tip.textColor = [UIColor whiteColor];
    _tip = tip;
//    tip.text = @"好友注册后你将获得5元现金红包";
    tip.font = FONT(customFontName, 14);
    [scroll addSubview:tip];
    [tip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(self.inviteCodeLabel.mas_bottom).offset(12);
    }];
    
    UIButton *wechat = [UIButton buttonWithType:UIButtonTypeCustom];
    [wechat setTitle:@"微信" forState:UIControlStateNormal];
    [wechat setTitleColor:UICOLOR_WITH_RGBINT(0xffffff) forState:UIControlStateNormal];
    [wechat setImage:IMAGE_NAMED(@"微信") forState:UIControlStateNormal];
    wechat.titleLabel.font = FONT(customFontName, 14);
    // button标题的偏移量
    wechat.titleEdgeInsets = UIEdgeInsetsMake(wechat.currentImage.size.height+5, -wechat.currentImage.size.width, 0,0);
    // button图片的偏移量
    wechat.imageEdgeInsets = UIEdgeInsetsMake(0,15,25,-15);
    [wechat addTarget:self action:@selector(shareToWechat:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *circle = [UIButton buttonWithType:UIButtonTypeCustom];
    [circle setTitle:@"朋友圈" forState:UIControlStateNormal];
    [circle setTitleColor:UICOLOR_WITH_RGBINT(0xffffff) forState:UIControlStateNormal];
    [circle setImage:IMAGE_NAMED(@"朋友圈") forState:UIControlStateNormal];
    circle.titleLabel.font = FONT(customFontName, 14);
    // button标题的偏移量
    circle.titleEdgeInsets = UIEdgeInsetsMake(circle.currentImage.size.height+5, -circle.currentImage.size.width, 0,0);
    // button图片的偏移量
    circle.imageEdgeInsets = UIEdgeInsetsMake(0,15,25,-25);
    [circle addTarget:self action:@selector(shareToCircle:) forControlEvents:UIControlEventTouchUpInside];
    
    [scroll addSubview:wechat];
    [scroll addSubview:circle];
    [wechat mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(-60);
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.top.equalTo(tip.mas_bottom).offset(0);
    }];
    [circle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(60);
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.top.equalTo(tip.mas_bottom).offset(0);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = UICOLOR_WITH_RGBINT(0xcccccc);
    [scroll addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.greaterThanOrEqualTo(wechat.mas_bottom).offset(0);
        make.leading.trailing.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(-44+(KIsiPhoneX?-34:0));
    }];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    btn.titleLabel.textColor = [UIColor whiteColor];
    btn.titleLabel.font = FONT(customFontName, 14);
    [btn addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
    [scroll addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(KIsiPhoneX?-34:0);
        make.leading.trailing.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
}

- (void)shareToWechat:(UIGestureRecognizer *)recog {
    NSString *code = [RGUserInfoModel getUserInfo].invitationCode;
    [XLMShareSdkTool shareInviteCodeWithShareContentType:SSDKPlatformTypeWechat content:SafeStr(code) onStateChanged:^(SSDKResponseState state) {
        if (state == SSDKResponseStateSuccess) {
            [self showTextMessageWithMessage:NSLocalizedString(@"分享成功", nil)];
        } else if (state == SSDKResponseStateFail) {
            [self showTextMessageWithMessage:NSLocalizedString(@"分享失败", nil)];
        }
    }];
    
}

- (void)shareToCircle:(UIGestureRecognizer *)recog {
    
    NSString *code = [RGUserInfoModel getUserInfo].invitationCode;
    [XLMShareSdkTool shareInviteCodeWithShareContentType:SSDKPlatformSubTypeWechatTimeline content:SafeStr(code) onStateChanged:^(SSDKResponseState state) {
        if (state == SSDKResponseStateSuccess) {
            [self showTextMessageWithMessage:NSLocalizedString(@"分享成功", nil)];
        } else if (state == SSDKResponseStateFail) {
            [self showTextMessageWithMessage:NSLocalizedString(@"分享失败", nil)];
        }
    }];
}

- (void)cancel:(UIButton *)btn {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)inviteRecords:(UIButton *)btn {
    RGInviteRecordsVC *vc = [[RGInviteRecordsVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)fuzhi:(UIButton *)btn {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = [RGUserInfoModel getUserInfo].invitationCode;
    [self showTextMessageWithMessage:@"复制成功"];
}

- (UIButton *)inviteRecordsBtn {
    if (!_inviteRecordsBtn) {
        _inviteRecordsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _inviteRecordsBtn.titleLabel.font = FONT(customFontName, 15);
        [_inviteRecordsBtn setTitle:@"邀请记录" forState:UIControlStateNormal];
        [_inviteRecordsBtn setTitleColor:UICOLOR_WITH_RGBINT(0x333333) forState:UIControlStateNormal];
        
        [_inviteRecordsBtn addTarget:self action:@selector(inviteRecords:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _inviteRecordsBtn;
}

- (UILabel *)inviteCodeLabel {
    if (!_inviteCodeLabel) {
        _inviteCodeLabel = [[UILabel alloc] init];
        _inviteCodeLabel.text = [NSString stringWithFormat:@"我的邀请码：%@",[RGUserInfoModel getUserInfo].invitationCode];
        _inviteCodeLabel.textColor = UICOLOR_WITH_RGBINT(0xffffff);
        _inviteCodeLabel.font = FONT(customFontName, 17);
    }
    return _inviteCodeLabel;
}

- (UIButton *)fuzhiBtn {
    if (!_fuzhiBtn) {
        _fuzhiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _fuzhiBtn.layer.cornerRadius = 10;
        _fuzhiBtn.layer.borderColor = UICOLOR_WITH_RGBINT(0xffffff).CGColor;
        _fuzhiBtn.layer.borderWidth = 1;
        [_fuzhiBtn setTitle:@"复制" forState:UIControlStateNormal];
        _fuzhiBtn.titleLabel.font = FONT(customFontName, 15);
        [_fuzhiBtn setTitleColor:UICOLOR_WITH_RGBINT(0xffffff) forState:UIControlStateNormal];
        [_fuzhiBtn addTarget:self action:@selector(fuzhi:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _fuzhiBtn;
}


@end
